<?php
namespace app\admin\controller\user;

use think\Db;
class Coupon extends \app\admin\controller\Common
{
    protected $type = 0;
    public function index(){
        return view();
    }

    public function list(){
        $limit = request()->param('limit') ?? 15;
        $page = request()->param('page');
        $page = $page ? ($page - 1) * $limit : 0;
        $search = request()->param();
        $query = (new \app\common\model\UserCoupon());
        isset($search['title']) && $search['title'] != "" ? $query->where("title like '%{$search['title']}%'") : '';
        $query->order("id desc");
        $list = $query->limit($page, $limit)
                ->append(['addtime_text','edntime_text','state_textt','type_text','tpye_text','user_username'])
                ->select();
        $count = $query->count();
   
        return self::successResult('',$list,$count);
    }

    public function add()
    {
        if (request()->isPost()) {
            $data = request()->param('info');
            $coupon = (new \app\common\model\Coupon())->where(['id' => $data['coupon_id']])->find();
            $arr = [];
            $user_arr = explode(',',$data['user_id']);
            foreach ($user_arr as $key => $value) {
                $arr[] = [
                    'title' => $coupon['title'],
                    'type' => $coupon['type'],
                    'money' => $coupon['money'],
                    'full_money' => $coupon['full_money'],
                    'user_id' => $value,
                    'goods_id' => $coupon['goods_id'],
                    'edntime' => time()+$coupon['limittime']*86400,
                    'coupon_id' => $coupon['id'],
                    // 'bus_id' => $coupon['bus_id']
                ];       
            }

            (new \app\common\model\UserCoupon())->saveAll($arr);
            return self::successResult();
        } else {
            return view();
        }
    }
    
    public function edit()
    {
        if (request()->isPost()) {
            $data = request()->param('info');
            $id = request()->param('id');
        
            $table = new \app\common\model\GoodsType();
            $row = $table->allowField(true)->save($data, ['id' => $id]);
            if ($row) {
                return self::successResult();
            } else {
                return self::errorResult();
            }
        } else {
            $id = intval(request()->param('id'));
            $info = (new \app\common\model\GoodsType())->where(['id' => $id])->find();
            $this->assign('info',$info);
   
            return view();
        }
    }

    public function delete(){
        $id = request()->param('id');
        if (empty($id)) $this->error("请选择要删除的数据");
        $table = new \app\common\model\GoodsType();
        if (is_array($id)) {
            $id = implode(",", $id);
            $row = $table->whereIn('id', $id)->delete();
        } else {
            $row = $table->where('id', $id)->delete();
        }
         return $row ? self::successResult("成功") : self::errorResult("失败");
    }

  

 
}
